// classe Produit
public class Produit
{
public string Nom {get; set;}
}
// classe Produit avec propriété
public class Produit
{
private string nom;
public string Nom
{
get {return nom;}
set {nom=value;}
}
}
// création et initialisation d’un produit
Produit prod; // variable de la page
.....
prod = new Produit() {Nom="Cirage"};
ze.DataContext = prod; // on spécifie le contexte de la zone d’édition
// classe Produit modifiée
using System.ComponentModel;
.....
public class Produit : INotifyPropertyChanged
{
private string nom;
public event PropertyChangedEventHandler PropertyChanged;
public string Nom
{
get {return nom;}
set
{
nom = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Nom"));
}
}
}
‘ version VB
Imports System.ComponentModel
Public Class Produit
Implements INotifyPropertyChanged
Private mNom As String
Public Property Nom() As String
Get
Return mNom
End Get
Set(ByVal value As String)
mNom = value
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Nom"))
End Set
End Property
Public Event PropertyChanged As PropertyChangedEventHandler _
Implements INotifyPropertyChanged.PropertyChanged
End Class
.....
Private prod As Produit
Private Sub LayoutRoot_Loaded(ByVal sender As System.Object, _
ByVal e As System.Windows.RoutedEventArgs)
prod = New Produit() With {.Nom = "Cirage"}
ze.DataContext = prod
End Sub
Boîte de liste
string[] ts = {"Portugal", "Suède", "Autriche"};
.....
Array.Sort(ts);
for (int i=0; i
public class Pays
{
public string Image {get; set; }
public string Nom {get; set;}
}
.....
List tabPays;
.....
tabPays = new List();
tabPays.Add(new Pays() { Image = "France.jpg", Nom = "France" });
.....
lbPays.ItemsSource = tabPays;
Public Class Pays
Private mImage As String
Public Property Image() As String
Get
Return mImage
End Get
Set(ByVal value As String)
mImage = value
End Set
End Property
Private mNom As String
Public Property Nom() As String
Get
Return mNom
End Get
Set(ByVal value As String)
mNom = value
End Set
End Property
End Class
.....
Private tabPays As List(Of Pays)
.....
tabPays = New List(Of Pays)()
tabPays.Add(New Pays() With {.Image = "France.jpg", .Nom = "France"})
.....
lbPays.ItemsSource = tabPays
using System.Collections.ObjectModel;
.....
ObservableCollection GetData()
{
ObservableCollection o = new ObservableCollection();
foreach (Pers p in tabPers) o.Add(p);
return o;
}
Imports System.Collections.ObjectModel
.....
Private Function GetData() As ObservableCollection(Of Pers)
Dim o As ObservableCollection(Of Pers) = New ObservableCollection(Of Pers)()
For Each p As Pers In tabPers
o.Add(p)
Next p
Return o
End Function
.....
void dg_LoadingRow(object sender, DataGridRowEventArgs e)
{
int N = e.Row.GetIndex(); // numéro de rangée en train d’être préparée
if (tabPers[N].AN > 1850) e.Row.Background = new SolidColorBrush(Colors.Red);
}